package com.gears_software.sxdc.dccx.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.gears_software.sxdc.dccx.R;
import com.gears_software.sxdc.dccx.application.DCCXApplication;
import com.gears_software.sxdc.dccx.data.finder.IBankFinder;
import com.gears_software.sxdc.dccx.data.finder.IVouchersFinder;
import com.gears_software.sxdc.dccx.data.finder.util.GearsErrorHandler;
import com.gears_software.sxdc.dccx.data.model.ResultModel;
import com.gears_software.sxdc.dccx.data.model.UserBalance;
import com.gears_software.sxdc.dccx.data.model.VouchersModel;
import com.gears_software.sxdc.dccx.ui.util.MaterialDialogProgress;
import com.github.mzule.activityrouter.annotation.Router;
import com.sdsmdg.tastytoast.TastyToast;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.App;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.rest.spring.annotations.RestService;
import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;

import qiu.niorgai.StatusBarCompat;

/**
 * Created by chezhenzhen on 2017/6/14.
 */

@EActivity(R.layout.activity_mywallet)
@Router(value = "wallet", isEActivity = true)
public class MyWalletActivity extends AppCompatActivity {

    @App
    DCCXApplication app;
    @ViewById
    TextView actionbar_title;
    @ViewById
    TextView back_icon;
    @ViewById
    TextView right_icon;
    @ViewById
    TextView txt_right_icon;
    @ViewById
    TextView txt_right_icon1;
    @ViewById
    TextView txt_right_icon2;
    @ViewById
    TextView txt_right_icon4;
    @ViewById
    TextView txt_chongzhi_icon;
    @ViewById
    TextView txt_daijinjuan_icon;
    @ViewById
    TextView txt_yaoqing_icon;
    @ViewById
    TextView txt_tx_icon;
    @ViewById
    RelativeLayout rl_daijinjuan;
    @ViewById
    TextView txt_liushui_icon;
    @ViewById
    TextView txt_yinhangka_icon;
    @ViewById
    TextView txt_liushui_right_icon;
    @ViewById
    TextView txt_yinhangka_right_icon;
    @ViewById
    TextView txt_vouchers_count;
    @ViewById
    RelativeLayout re_chongzhi;
    @ViewById
    RelativeLayout re_yinhangka;
    @ViewById
    TextView tv_chongzhi_money;
    @ViewById
    TextView tv_tixian_money;
    @RestService
    IBankFinder bankFinder;
    @RestService
    IVouchersFinder vouchersFinder;
    @Bean
    GearsErrorHandler errorHandler;
    List<VouchersModel> list = new ArrayList<VouchersModel>();
    MaterialDialogProgress progress;
    String statusid = "1";

    @AfterViews
    void created() {
        bankFinder.setRestErrorHandler(errorHandler);
        vouchersFinder.setRestErrorHandler(errorHandler);
        initIconFont();
        StatusBarCompat.setStatusBarColor(this, ContextCompat.getColor(this, R.color.colorActionbar));
        progress = new MaterialDialogProgress(this);
        progress.show("正在加载...");
        getUserBalance();
        getVoucherCount();
    }

    void initIconFont() {
        actionbar_title.setText("我的钱包");
        back_icon.setVisibility(View.VISIBLE);
        back_icon.setText(R.string.back_icon);
        back_icon.setTypeface(app.iconfont);
        right_icon.setVisibility(View.GONE);
        txt_right_icon.setTypeface(app.iconfont);
        txt_right_icon1.setTypeface(app.iconfont);
        txt_right_icon2.setTypeface(app.iconfont);
        txt_right_icon4.setTypeface(app.iconfont);
        txt_chongzhi_icon.setTypeface(app.iconfont);
        txt_daijinjuan_icon.setTypeface(app.iconfont);
        txt_yaoqing_icon.setTypeface(app.iconfont);
        txt_tx_icon.setTypeface(app.iconfont);
        txt_liushui_icon.setTypeface(app.iconfont);
        txt_liushui_right_icon.setTypeface(app.iconfont);
        txt_yinhangka_icon.setTypeface(app.iconfont);
        txt_yinhangka_right_icon.setTypeface(app.iconfont);
    }

    @Click
    void re_chongzhi() {
        Intent intent = new Intent(this, ChongZhiActivity_.class);
        startActivity(intent);
    }

    @Click
    void re_tx() {
        Intent intent = new Intent(this, YuETiXianActivity_.class);
        startActivity(intent);
    }

    @Click
    void re_liushui() {
        Intent intent = new Intent(this, JiaoYiLiuShuiActivity_.class);
        startActivity(intent);
    }

    @Click
    void re_yinhangka() {
        Intent intent = new Intent(this, BankCardActivity_.class);
        startActivity(intent);
    }

    @Click
    void rl_daijinjuan() {
        Intent intent = new Intent(this, VouchersActivity_.class);
        startActivity(intent);
//        TastyToast.makeText(this, "敬请期待", app.SHORT, app.WARNING);
    }

    @Click
    void back_icon() {
        this.finish();
    }

    @Background
    public void getUserBalance() {
        ResultModel<UserBalance> res = bankFinder.getUserBalance();
        showResout(res);
    }

    @UiThread
    void showResout(ResultModel<UserBalance> res) {
        progress.dismiss();
        if (res != null) {
            if (res.getErrno().equals("0")) {
                if (res.getData() != null) {
                    UserBalance balance = res.getData();
                    tv_chongzhi_money.setText("¥" + balance.getUseramount());
                    tv_tixian_money.setText("¥" + balance.getCarpoolamount());
                }
            }
        }
    }

    @Background
    void getVoucherCount() {
        ResultModel<List<VouchersModel>> res = vouchersFinder.getVouchersList(statusid);
        showVoucherResult(res);
    }

    @UiThread
    void showVoucherResult(ResultModel<List<VouchersModel>> res){
        if (res != null){
            if (res.getErrno().equals("0")){
                if (res.getData() != null){
                    list = res.getData();
                    if (list.size()>0){
                        txt_vouchers_count.setText(list.size() + "张");
                    }
                }
            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        getUserBalance();
    }
}
